from openpyxl import load_workbook
from openpyxl import Workbook
import os
import random
folder_path = 'd:/data/'
for root, dirs, files in os.walk(folder_path):
    for file1 in files:
        ####文件路径
        file_path = os.path.join(root, file1)
        print(file_path)
        if '.xlsx' in file_path:
            wb = load_workbook(file_path)
            ws=wb.active
            print("开始处理")
            max_row=ws.max_row+1
            TIME,TIMEE="","" 
            for row in range(10,max_row):
                #如果第一列不为空
                print(str(ws.cell(row, 1).value)[5:7])
                if ws.cell(row, 1).value is not None:
                    if str(ws.cell(row, 1).value)[5:7]=="10":
                        if ws.cell(row,4).value!=TIME or ws.cell(row, 1).value != TIMEE:
                            TIME=ws.cell(row,4).value
                            TIMEE=ws.cell(row,1).value
                            #在当前行插入一行
                            ws.insert_rows(row)
                            ws.cell(row, 1).value = ws.cell(row+1, 1).value
                            ws.cell(row, 2).value = ws.cell(row+1, 2).value
                            ws.cell(row, 3).value = "请补充SN"
                            ws.cell(row, 4).value = ws.cell(row+1, 4).value
                            for i in range(5, ws.max_column + 1):
                                if ws.cell(row + 1, i).value is not None:
                                    ws.cell(row, i).value = round(ws.cell(row + 1, i).value+random.uniform(0, 0.01),3)
                    row += 1
                    max_row+=1

            wb.save(file_path)
            print("保存成功")



